/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import sunsite.po.Role;
import sunsite.po.Sai;
import sunsite.po.University;
import sunsite.po.Usr;
import sunsite.service.DatadicService;
import sunsite.service.SaiService;
import sunsite.service.UniversityService;
import sunsite.service.UserService;
import sunsite.tools.ContextHelper;
import sunsite.tools.MD5;
import sunsite.tools.Naming;
import sunsite.vo.UserInfo;

/**
 *
 * @author Ruby
 */
public class Register extends ActionSupport {

    public static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Register.class);
    //请求参数
    private String type;
    private String userName;
    private String fullName;
    private String password;
    private String email;
    private String phone;
    private String university;
    private String degree;
    private String major;
    private String grade;
    private String sai;
    private String saiCode;
    private String userGroup;
    private String verifyCode;
    //传递参数
    private String message;
    private List<sunsite.po.University> universities;
    private List<sunsite.po.Sai> sais;
    private List gradeList;
    private List degrees;
    private ResourceBundleMessageSource messageSource;
    private UserService userService;
    private UniversityService uniService;
    private SaiService saiService;
    private DatadicService datadicService;

    public Register() {
        
    }

    @Override
    public String execute() throws Exception {
        if (type != null && type.equals("getForm")) {
            universities = uniService.getUniversityList();
            sais = saiService.getSaiList();
            gradeList = datadicService.getDatadicValues("grade");
            degrees = datadicService.getDatadicValues("degree");
            return INPUT;
        }

        if (type != null && type.equals("submit")) {
            HttpServletRequest request = ServletActionContext.getRequest();
//            request.setCharacterEncoding("GBK");
            String requestCode = request.getParameter(Naming.WebParameter.VerifyCode);
            String sessionCode = (String) request.getSession().getAttribute(Naming.SessionName.VerifyCodeSession);
            if (sessionCode == null || !sessionCode.equals(requestCode)) {
                message = messageSource.getMessage("user.verifyCodeError", null, Locale.getDefault());
                return "error";
            }

            if (userName == null || password == null || fullName == null || email == null) {
                message = messageSource.getMessage("user.regInfoNotEnought", null, Locale.getDefault());
                return "error";
            }
            Usr user = new Usr();
            user.setUserName(userName);
            user.setFullName(fullName);
            user.setPassword(password);
            user.setEmail(email);

            user.setLastLogin(new java.util.Date());
            user.setLoginCount(0);
            user.setRegistTime(new java.util.Date());
            user.setUserId(sunsite.tools.UserIdentifier.newUserIdentifier());
            user.setDegree(degree == null ? "" : degree);
            user.setMajor(major == null ? "" : major);
            user.setPhone(phone == null ? "" : phone);
            user.setStatus(userGroup == null ? 1 : (userGroup.equals("true") ? 2 : 1));

            if (sai != null && sai.length() > 0 && !sai.equals("-1")) {
                user.setSai(new Sai(Integer.valueOf(sai)));
            }
            if (university != null && university.length() > 0 && !university.equals("-1")) {
                user.setUniversity(new University(Integer.valueOf(university)));
            }
            user.setRole(new Role(1));

            try {
                userService.register(user);
                //message = messageSource.getMessage("user.register", null, Locale.getDefault());
                //message += " 用户名：" + userService.getUsrByName(userName).getUserName();
                ContextHelper.getSession().put(Naming.SessionName.UserSession, new UserInfo(user.getUserId(), user.getUserName(), user.getFullName(), user.getRole().getRoleId(), user.getRole().getRoleName()));
//                message += request.getParameter("degree");
            } catch (Exception err) {
                logger.error(err);
                message = err.toString();
                return "error";
            }

            return SUCCESS;
        }

        return SUCCESS;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setUniversity(String university) {
        this.university = university;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public void setSai(String sai) {
        this.sai = sai;
    }

    public void setSaiCode(String saiCode) {
        this.saiCode = saiCode;
    }

    public void setUserGroup(String userGroup) {
        this.userGroup = userGroup;
    }

    public String getMessage() {
        return message;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<sunsite.po.University> getUniversities() {
        return universities;
    }

    public void setUniService(UniversityService uniService) {
        this.uniService = uniService;
    }

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }

    public List<sunsite.po.Sai> getSais() {
        return sais;
    }

    public void setSaiService(SaiService saiService) {
        this.saiService = saiService;
    }

    public void setDatadicService(DatadicService datadicService) {
        this.datadicService = datadicService;
    }

    public List<sunsite.po.Datadic> getGradeList() {
        return gradeList;
    }

    public List getDegrees() {
        return degrees;
    }
}